<?php

namespace Admin\Controller;
use Admin\Model\CountryModel;

/**
 * Class CountryController
 * 国家管理模块
 *
 * @package Admin\Controller
 * @author janatian
 */
class CountryController extends SystemController
{
    /**
     * 搜索
     *
     * @param $map
     */
	public function _filter(&$map)
	{
	    $_search = search_map();
	    if (isset($_search['abbr2'])) {
            $abbr2 = array_filter(explode(',', trim($_search['abbr2'])));
	        $_search['abbr2'] = ['IN', $abbr2];
	    }
        if (isset($_search['name'])) {
            $_search['name'] = ['LIKE', '%' . $_search['name'] . '%'];
        }
	    $map = !empty($_search) ? array_merge($_search, $map) : $map;
	}

    public function _before()
    {
        $enabledArr = [
            CountryModel::CODE_ENABLED_ON => "启用",
            CountryModel::CODE_ENBALED_OFF => "禁用"
        ];
        $this->assign('enabled', $enabledArr);
    }

    public function _before_insert()
    {
        array_map('trim', $_POST);
        if (empty($_POST['abbr2']) || 2 != strlen($_POST['abbr2']))
        {
            $this->error('abbr2 非法, 请确认是否两位英文字母');
        }
        if (!empty($_POST['abbr3']) && 3 != strlen($_POST['abbr3']))
        {
            $this->error('abbr3 非法, 请确认是否三位英文字母');
        }
        $_POST['abbr2'] = strtolower($_POST['abbr2']);
        $_POST['abbr3'] = strtolower($_POST['abbr3']);
        $_POST['name'] = ucfirst($_POST['name']);
        $_POST['enabled'] = intval($_POST['enabled']);

        $exist = D('Admin/Country')->where(['abbr2'=>$_POST['abbr2']])->find();
        if (!empty($exist))
        {
            $this->error("{$_POST['abbr2']} 对应记录已存在");
        }
    }

    public function _before_update()
    {
        array_map('trim', $_POST);
        if (empty($_POST['abbr2']) || 2 != strlen($_POST['abbr2']))
        {
            $this->error('abbr2 非法, 请确认是否两位英文字母');
        }
        if (!empty($_POST['abbr3']) && 3 != strlen($_POST['abbr3']))
        {
            $this->error('abbr3 非法, 请确认是否三位英文字母');
        }
        $_POST['abbr2'] = strtolower($_POST['abbr2']);
        $_POST['abbr3'] = strtolower($_POST['abbr3']);
        $_POST['name'] = ucfirst($_POST['name']);
    }

    public function enabledUpdate()
    {
        $ids   = explode(',', $_REQUEST['ids']);
        $enabled = intval($_REQUEST['enabled']);
        $model = D('Admin/Country');
        try {
            $model->where(['id'=>['IN', $ids]])->save(['enabled'=>$enabled]);
            $rows = $model->where(['id'=>['IN', $ids], 'enabled'=>$enabled])->getField('id,abbr2',true);
            $diff = array_diff($ids, array_keys($rows));
            if ($diff)
            {
                foreach ($diff as $i)
                {
                    $vals[] = $rows[$i];
                }
                throw new \Exception('更新失败:' . implode(',', $vals) . ' 请重试');
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        D('Admin/Country')->getAllGeoAbbr2(true);
        $this->success('更新成功');
    }

    public function _after_insert()
    {
        D('Admin/Country')->getAllGeoAbbr2(true);
    }

    public function _after_update()
    {
        D('Admin/Country')->getAllGeoAbbr2(true);
    }
}